<template>
  <a-modal
    :title="title"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        
        <a-form-model-item 
          :labelCol="labelCol" 
          :wrapperCol="wrapperCol" 
          prop="url"
          required
          label="背景图">
          <JImageUpload 
            :uploadParams="getUploadParams()" 
            :isMultiple="true" 
            :fileSize="0.5"
            :number="1" 
            :accept="'image/*'"   
            @fileChange="fileImageChange" 
            v-model="fileList"
          ></JImageUpload>
          <div class="upload__tip" style="line-height: 16px;">建议尺寸宽度750px，上传2M以内jpg、png格式图片</div>
        </a-form-model-item>

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-textarea placeholder="请输入" v-model="model.sort" :rows="4" />
        </a-form-model-item>

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="status"
          required
          label="状态">
          <a-radio-group v-model="model.status" name="radioGroup">
            <a-radio :value="0">启用</a-radio>
            <a-radio :value="1">禁用</a-radio>
          </a-radio-group>
        </a-form-model-item>

      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { saveOrUpdateAdvert } from '@/api/marketingCenter/operations'
  import globalData from '@/assets/js/common'
  import JImageUpload from '@/components/qiniuUpload/JImageUpload.vue'

  export default {
    name: 'banner',
    components: { JImageUpload },
    props:{
      typeNum: {
        type: String,
        default: ''
      },
    },
    data() {
      return {
        fileList:[],
        value: 1,
        title: '操作',
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        filteredOptions:[
          {
            id:1,
            name:'品牌1',
          },
          {
            id:2,
            name:'品牌2',
          },
        ],
        validatorRules: {
          title: [{ required: true, message: '请选择品牌!' }],
          status: [{ required: true, message: '请选择状态!' }],
          }
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({})
      },
      edit(record) {
        if (record.id) {
          this.visiblekey = true
        } else {
          this.visiblekey = false
        }
        this.model = Object.assign({}, record)
        this.fileList = record.url
        // this.fileList = [record.url]
        this.visible = true
      },
      // 上传图片
      getUploadParams() {
        return { ...globalData.uploadParams }
      },
      fileImageChange(list) {
        this.model.url = list.length > 0 ? list[0].url : []
      },
      // 品牌选择
      handleChange() {

      },
      // 确定
      handleOk() {
        const that = this
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true
            let obj
            Object.assign(this.model,{'type':this.typeNum})
            if (!this.model.id) {
              obj = saveOrUpdateAdvert(this.model)
            } else {
              obj = saveOrUpdateAdvert(this.model)
            }
            obj.then((res) => {
              if (res.success) {
                that.$message.success(res.msg)
                that.$emit('ok')
              } else {
                that.$message.warning(res.msg)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }else{
            return false;
          }
        })
      },
      // 关闭
      handleCancel() {
        this.close()
      },
      close() {
        this.$emit('close')
        this.fileList = []
        this.visible = false
        this.$refs.form.resetFields();
      }
    }
  }
</script>